# -*- coding: utf-8 -*-
# created by Alberthch - 陈航  2021/7/15 19:07
# Description:您想创建一个字典，并且在迭代或者序列化这个字典的时候能够控制元素的顺序
# 解决方案：使用collection.OrderDict模块创建字典，但是内存消耗大于普通字典
from collections import OrderedDict


def print_dict(dict_):
    print("=" * 20)
    for key, value in dict_.items():
        print(key, value)


# 字典的无序是指数据存进字典的顺序跟取出字典的顺序不一致
dict_1 = OrderedDict()
dict_1["foo"] = 1
dict_1["bar"] = 2
dict_1["sapm"] = 3
dict_1["grok"] = 4
print_dict(dict_1)


dict_2 = dict()
dict_2["foo"] = 1
dict_2["bar"] = 2
dict_2["sapm"] = 3
dict_2["grok"] = 4
print_dict(dict_2)